/**
 * @param {number} capacity
 */
 var LRUCache = function(capacity) {
    this.map = new Map()
    this.capacity = capacity
    this.count = 0
};

/** 
 * @param {number} key
 * @return {number}
 */
LRUCache.prototype.get = function(key) {
    if(this.map.has(key)){
        this.map.get(key).num  += 1
        return this.map.get(key).value
    }else{
        return -1
    }
   
};

/** 
 * @param {number} key 
 * @param {number} value
 * @return {void}
 */
LRUCache.prototype.put = function(key, value) {
    if(this.count >= this.capacity){
        // 当存储数量大于最大容量时，需要去除访问量最少的
        let min = -1
        let targetKey ; 
        for (const [key, item] of this.map) {
            if(min === -1){
                min = item.num 
                targetKey = key
            }else{
                if(item.num < min){
                    targetKey = key
                }
            }
        }
        this.map.delete(targetKey)
    }
    if(!this.map.has(key)){
        this.map.set(key, {num: 0, value:value})
         this.count += 1
    }
    
};

/**
 * Your LRUCache object will be instantiated and called as such:
 * var obj = new LRUCache(capacity)
 * var param_1 = obj.get(key)
 * obj.put(key,value)
 */